﻿/***************************************************************
 * 开发人员：网魂小兵@http://www.mozlite.com
 * 
 * 缓存配置节点类。
 * 
 * 最后修改：2011-10-28
 * ************************************************************/
namespace Mozlite.Caching
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Configuration;
    using System.IO;
    using System.Web;
    /// <summary>
    /// 缓存配置节点类。
    /// </summary>
    public class CacheConfigurationSection : ConfigurationSection
    {
        private static readonly object locker = new object();
        private static string path = null;
        private static volatile CacheConfigurationSection section = null;
        /// <summary>
        /// 刷新配置。
        /// </summary>
        public static void Refresh()
        {
            lock (locker)
            {
                section = null;
                CacheConfigurationSection current = Current;
            }
        }

        /// <summary>
        /// 获取或设置备份HTTP运行时缓存绝对过期时间（秒），默认为5秒。
        /// </summary>
        [ConfigurationProperty("backupHttpRuntimeCacheAbsoluteExpirationSeconds", DefaultValue = 5)]
        public int BackupHttpRuntimeCacheAbsoluteExpirationSeconds
        {
            get
            {
                return Convert.ToInt32(base["backupHttpRuntimeCacheAbsoluteExpirationSeconds"]);
            }
            set
            {
                base["backupHttpRuntimeCacheAbsoluteExpirationSeconds"] = value;
            }
        }
        /// <summary>
        /// 获取缓存配置文件物理路径。
        /// </summary>
        public static string CachingConfigurationFilePath
        {
            get
            {
                if (path == null)
                {
                    HttpContext current = HttpContext.Current;
                    path = (current != null) ? current.Server.MapPath("~/cache.config") : Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "cache.config");
                }
                return path;
            }
        }
        /// <summary>
        /// 获取当前节点实例对象。
        /// </summary>
        public static CacheConfigurationSection Current
        {
            get
            {
                lock (locker)
                {
                    if (section == null)
                    {
                        string cachingConfigurationFilePath = CachingConfigurationFilePath;
                        if (File.Exists(cachingConfigurationFilePath))
                        {
                            ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
                            fileMap.ExeConfigFilename = fileMap.MachineConfigFilename = cachingConfigurationFilePath;
                            System.Configuration.Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
                            if (configuration != null)
                            {
                                section = (CacheConfigurationSection)configuration.GetSection("cache");
                            }
                        }
                        else
                        {
                            section = new CacheConfigurationSection();
                        }
                    }
                    return section;
                }
            }
        }
        /// <summary>
        /// 获取或设置默认提供者，默认值为“HttpRuntimeCacheProvider”。
        /// </summary>
        [ConfigurationProperty("defaultProvider", DefaultValue = "HttpRuntimeCacheProvider"), StringValidator(MinLength = 1)]
        public string DefaultProvider
        {
            get
            {
                return (string)base["defaultProvider"];
            }
            set
            {
                base["defaultProvider"] = value;
            }
        }
        /// <summary>
        /// 获取提供者集合。
        /// </summary>
        [ConfigurationProperty("providers")]
        public ProviderSettingsCollection Providers
        {
            get
            {
                return (ProviderSettingsCollection)base["providers"];
            }
        }
    }
}
